﻿using ADODB;
using fuzhu;
using LH.Model.ST;
using SqlSuarEntity;
using SqlSugar;
using System.Data;
using System.Runtime.ConstrainedExecution;
using System.Security.Cryptography;
using U8COWeb;
using XXX.Bo;
using XXX.Common;
using XXX.Models;
using XXX.Models.AS;
using XXX.Models.XXXEntities;
using XXX.Models.ZJGLX;
using ZDEntity;

namespace Yonyou.U8.BO
{
    /// <summary>
    /// 客户存货对照表
    /// </summary>
    public class CusInvContraposeVM  
    {
        //当前的数据库连接字符串
        public string conStr { get; set; }
        public string xmlConfig { get; set; }
        public bool bWMS = false;
        public string cDatabase = "";
        public CusInvContrapose entity ;

        public CusInvContraposeVM()
        {
            //conStr
            conStr = CanShu.conStr;
            //初始化定义xmlconfig
            xmlConfig = XmlOperation.GetConfigXML();
        }
        public RetModel2 AddList(CusInvContraposelist lst)
        {
            try
            {
                if (lst == null)
                {
                    throw new Exception("参数为空");
                }
                if (lst.itemList == null)
                {
                    throw new Exception("参数为空");
                }

                //循环添加
               List<CusInvInsertResult> cusInvInserts = new List<CusInvInsertResult>();
                bool bsuccess = true;
                for (int i = 0; i < lst.itemList.Count; i++)
                {
                   entity=lst.itemList[i];
                    RetModel2 ret =Add();
                    CusInvInsertResult cusInvInsertResult = new CusInvInsertResult();
                    cusInvInsertResult.cCusCode = lst.itemList[i].cCusCode;
                    cusInvInsertResult.cInvCode = lst.itemList[i].cInvCode;
                    cusInvInsertResult.cReCode =  ret.code;
                    cusInvInsertResult.cResult = ret.message;
                    cusInvInserts.Add(cusInvInsertResult);
                    if (ret.code!=200)
                    {
                        bsuccess = false;
                    }
                }
                if (bsuccess)
                {
                    return DBService.ReOKObject(cusInvInserts);
                }
                else
                {
                    return DBService.ReErrObjectData("导入有错误",cusInvInserts);
                }
               
                //}
            }
            catch (Exception ex)
            {
                return DBService.ReErrObject(ex.Message);
            }
        }


        public RetModel2 Add()
        {
            try
            {
                string idcheck = "";

               CustomerVM vM = new CustomerVM();
                if (!vM.Exists(entity.cCusCode))
                {
                    return DBService.ReErrObject("U8中无次客户编码"+ entity.cCusCode);
                }
                InventoryVM inventoryVM =   new InventoryVM();
                if (!inventoryVM.Exists(entity.cInvCode))
                {
                    return DBService.ReErrObject("U8中无存货编码"+ entity.cInvCode);
                }
                 //检查是否已经存在
                if(Exists(entity.cCusCode, entity.cInvCode))
                {
                    //return DBService.ReErrObject("当前已有此对照：客户"+ entity.cCusCode + "存货" + entity.cInvCode);
                    //进行更新处理
                    using (SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
                    {
                        ConnectionString = conStr,
                        DbType = SqlSugar.DbType.SqlServer,
                        IsAutoCloseConnection = true
                    }))
                    {

                        var result = db.Updateable<CusInvContrapose>()
               .SetColumns(it => new CusInvContrapose()
               {
                   cCusInvCode = entity.cCusInvCode,
                   cCusInvName = entity.cCusInvName
               })
               .Where(it => it.cInvCode == entity.cInvCode && it.cCusCode == entity.cCusCode)
               .ExecuteCommand();


                    }
                }
                 else
                {
                    using (SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
                    {
                        ConnectionString = conStr,
                        DbType = SqlSugar.DbType.SqlServer,
                        IsAutoCloseConnection = true
                    }))
                    {
                        entity.cCIGUID = Guid.NewGuid();
                        entity.bOutInvDT = false;
                        int ire=   db.Insertable<CusInvContrapose>(entity).ExecuteCommand();


                    }
                }
              
                return DBService.ReOKObject("添加完成");
                //}
            }
            catch (Exception ex)
            {
                return DBService.ReErrObject(ex.Message);
            }



        }


 


        /// <summary>
        /// 检查单据号是否存在
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool Exists(string ccuscode,string cinvcode)
        {
           
            
            using (SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
            {
                ConnectionString = conStr,
                DbType = SqlSugar.DbType.SqlServer,
                IsAutoCloseConnection = true
            }))
            {
               bool bexists =  db.Queryable<CusInvContrapose>().Where(x => x.cCusCode == ccuscode && x.cInvCode == cinvcode).Any();
               return bexists;
    
            } 
        
        }

         
    }
}
